var CONFIG_PARSER = {};
var ad_config = {};

CONFIG_PARSER.checkPageIsAdult = function(){
  var uri = window.location.pathname;
  for(var i in AD_CONFIG.wordBlacklist){
    if(uri.toLowerCase().indexOf(AD_CONFIG.wordBlacklist[i].toLowerCase()) > -1){
      return true;
    }
  }
  return false;
}

CONFIG_PARSER.setEvent = function(ab){
  if(!ga){
    setTimeout(function () {
      CONFIG_PARSER.setEvent();
    }, 100);
  }
  else{
    var dimensionValue = ab + CONFIG_PARSER.getInclusion();
    ga('set', 'dimension3', dimensionValue);
    ga("send", "event", "ab-test", "AB-TEST " + ab, { nonInteraction: true });
    document.cookie = "ab-value=" + ab + "; domain=" + window.location.host +"; path=/";
  }
}

CONFIG_PARSER.getInclusion = function(config) {
  if (typeof(I11C) !== "undefined" && I11C.Morph == 1) {
    return "_Instart";
  } else {
    return "";
  }
}


CONFIG_PARSER.getSiteAB = function() {
  if (!AD_CONFIG.active) {
    return configA;
    CONFIG_PARSER.setEvent('A');
  }
  var sessionKey = 'adAB-' + AD_CONFIG.id;

  var ab = $WH.sessionStorage.get(sessionKey);
  if (AD_CONFIG.abTestOptions.indexOf(ab) < 0) {
    if (Math.random() < AD_CONFIG.bPercent * .01) {
      ab = 'B';
    } else {
      ab = 'A';
    }
    $WH.sessionStorage.set(sessionKey, ab);
  }
  CONFIG_PARSER.setEvent(ab);
  if(ab == 'A') return configA;
  if(ab == 'B') return configB;
};

CONFIG_PARSER.GO = function(){
  ad_config = CONFIG_PARSER.getSiteAB();
  if(!CONFIG_PARSER.checkPageIsAdult()) {
    if(ad_config.type == "MonkeyBroker"){
      mb_parser.parse(ad_config);
    }
    if(ad_config.type == "Google"){
      google_parser.parse(ad_config);
    }
  }
}
